package demo2_runnable;

public class Main {
    public static void main(String[] args) {
        //创建线程任务对象
        MyRunnable myRunnable=new MyRunnable();
        //创建线程类，并将任务传给thread
        Thread thread1=new Thread(myRunnable);
        Thread thread2=new Thread(myRunnable);
        //启动线程
        thread1.start();
        thread2.start();
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Main方法正在执行");
        }
    }
}
//这时候定义的是线程的执行任务，并不是定义线程类
class MyRunnable implements Runnable{
    @Override
    public void run() {
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Runnable中的run方法正在执行");
        }
    }
}
